
var express = require('express');
const router = express.Router();
const user=require('../model/user')

const rooms =require('../model/rooms');
const roomusers=require('../model/roomuser')
const  message=require('../model/message')



router.post('/login',async (req, res) => {
    const myuser={
        name: req.body.name,
        password: req.body.password
    }

    if (myuser.name == '' || myuser.password == '') {
        res.status(500).send('用户名或密码不能为空');
    }
    try {
        const result = await user.findOne({
            where: {
                name: myuser.name,
                password: myuser.password
            }
        });
            if(result!=null)
        res.status(200).send('登录成功')
        else {
            res.status(500).send('用户名或密码错误');
        }
    }
    catch (e)
    {
        console.log(e)
        res.status(500).send('用户名或密码错误');
    }


})

router.post('/registry',async (req, res) => {

    const myuser = {
        name: req.body.name,
        password: req.body.password
    }

    if (myuser.name == '' || myuser.password == '') {
        res.status(500).send('用户名或密码不能为空');
    }
    try {
        const result = await user.create(myuser)
        if (result != null)
            res.status(200).send('注册成功')
        else {
            res.status(500).send('注册失败');
        }
    } catch (error) {
        console.log(error)
        res.status(500).send('注册失败');
    }
})

router.post('/createRoom',async (req,res)=>{
    const  room={
        roomname:string=req.body.roomname,
        personall :number=req.body.personall,
        persononline:number=req.body.persononline,
        owner:number=req.body.owner
    }
    console.log(room)
    try {
        const result=await rooms.create(room);
        console.log(result)
        if(result!=null)
            if (result != null)
                res.status(200).send('创建成功')
            else {
                res.status(500).send('创建失败');
            }
    }
    catch (error) {
        console.log(error)
        res.status(500).send('创建失败');
    }
})

router.post('/joinRooms',async(req,res) =>{
    console.log(req.body)
    // console.log(req)

    try
    {
        const  result=await roomusers.create({
            roomname:req.body.roomname,
            name:req.body.name,
            online:req.body.online
        })
        console.log(result)
        if (result != null)
            res.status(200).send('加入成功')
        else {
            res.status(500).send('加入失败');
        }
    } catch (error) {
        console.log(error)
        res.status(500).send('加入失败');
    }

})
router.post('/showrooms',async (req,res)=>{
    try {
        const result=await roomusers.findAll({
            where :
                {
                    name:req.body.owner
                }
        })
        if(result!=null)
            res.status(200).send(result)
        else {
            res.status(500).send('获取失败');
        }
    }catch (e)
    {
        console.log(e+"获取失败")
    }

})
router.get('/getuser',async (req,res)=>{
    const roomname=req.params.roomname;
    try {
        const result=await roomuser.findAll({
            where:{
                roomname:roomname
            }
        })
        console.log(result)
        if(result!=null)
            res.status(200).send(result)
        else {
            res.status(500).send('获取失败');
        }
    }
    catch (e)
    {
        res.status(500).send('获取失败')
    }
})
router.get('/message/getMessages',async(req,res)=>{
    // console.log(req)
    const roomname=req.query.roomname;
    try {
        const result=await message.findAll({
            where:{
                roomname:roomname
            }
        })
        if(result!=null)
            res.status(200).send(result)
        else {
            res.status(500).send('获取失败');
        }
    }
    catch (e)
    {
        res.status(500).send('获取失败')
    }
})

module.exports=router;
